function[]=alg(akce)
%ALG  calls up a window for estimation of optimal smoothing parameters
%
%     Use only in procedure KERN
%
% (C) Jan Kolacek, Masaryk University (Czech Republic)

col=[0.831373 0.815686 0.784314]; %[.9 .9 .9]
efig=findobj(0,'name','EDIT');
hndlList=get(efig,'UserData');

pos=get(efig,'Position');
eHndl=hndlList(11);
vel=get(eHndl,'position');
pos=[pos(1:2)+vel(1:2).*pos(3:4) vel(3:4).*pos(3:4)-[0 .025]];

switch akce
    case 'start'
        rozh=findobj(0,'Name','Set a method');
        if ~isempty(rozh)
            figure(rozh);
        else
            figure('Units','normalized','Name','Set a method','Color',col,'Tag','eclos',...
                'menubar','none','resize','off','Numbertitle','off','Position',pos);
            set(gcf,'closerequestfcn','alg(''close'')');
            infstr=['Choose the type of the estimator'];
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'Position',[0,.93,1,.07], ...
                'HorizontalAlignment','center',...
                'String',infstr);
            
            c1='hL=get(gcf,''UserData'');alg(''next'');';
            c2='set(hL(2:4),''value'',0);set(hL(1),''value'',1);';
            nwHndl=uicontrol('Style','radiobutton','Units','normalized', ...
                'FontSize',round(pos(4)*10)+6, ...
                'FontUnits','normalized',...
                'Position',[.05,.85,.4,.05], ...
                'Callback',[c1,c2],...
                'String','Nadaraya - Watson');
            
            c2='set(hL([1,3,4]),''value'',0);set(hL(2),''value'',1);';
            llHndl=uicontrol('Style','radiobutton','Units','normalized', ...
                'FontSize',round(pos(4)*10)+6, ...
                'FontUnits','normalized',...
                'Position',[.05,.79,.4,.05], ...
                'Callback',[c1,c2],...
                'String','Local - linear');
            
            c2='set(hL([1,2,4]),''value'',0);set(hL(3),''value'',1);';
            gmHndl=uicontrol('Style','radiobutton','Units','normalized', ...
                'FontSize',round(pos(4)*10)+6, ...
                'FontUnits','normalized',...
                'Position',[.55,.85,.4,.05], ...
                'Callback',[c1,c2],...
                'String','Gasser - Mueller');
            
            c2='set(hL(1:3),''value'',0);set(hL(4),''value'',1);';
            pchHndl=uicontrol('Style','radiobutton','Units','normalized', ...
                'FontSize',round(pos(4)*10)+6, ...
                'FontUnits','normalized',...
                'Position',[.55,.79,.4,.05], ...
                'Callback',[c1,c2],...
                'String','Priestley - Chao');
            hL=[nwHndl llHndl gmHndl,pchHndl,0];
            set(gcf,'UserData',hL);
        end;
        
    case 'next'
        hL=get(gcf,'UserData');
        if hL(5)==0
            hL(5)=1;
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'Position',[0,.67,1,.07], ...
                'HorizontalAlignment','center',...
                'String','Set a method for choosing of optimal h');
            
            %1.sloupec  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            uicontrol( ...
                'Style','frame', ...
                'Units','normalized', ...
                'FontUnits','normalized',...
                'Position',[.04,.3,.58,.35], ...
                'BackgroundColor',col);
            
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'Position',[.05,.57,.56,.07], ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'BackgroundColor',col,...
                'String','Penalizing functions');
            
            kraj=[0.05*ones(1,4),.34*ones(1,3)];
            nazev=char('Akaike','FPE','Full','GCV','Kolacek','Rice','Shibata');
            c1='hL=get(gcf,''UserData'');set(hL(17),''Enable'',''on'');';
            
            for i=1:7
                c2='set(hL(6:16),''value'',0);set(gco,''value'',1);';
                hL(5+i)=uicontrol('Style','radiobutton','Units','normalized', ...
                    'FontSize',round(pos(4)*10)+6, ...
                    'FontUnits','normalized',...
                    'Position',[kraj(i),.5-mod(i-1,4)*.06,.27,.05], ...
                    'Callback',[c1,c2],...
                    'String',nazev(i,:));
            end;
            
            %2.sloupec  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            
            uicontrol( ...
                'Style','frame', ...
                'Units','normalized', ...
                'FontUnits','normalized',...
                'Position',[.64,.3,.32,.35], ...
                'BackgroundColor',col);
            
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'Position',[.65,.57,.3,.07], ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'BackgroundColor',col,...
                'String','Others');
            
            kraj=0.65;
            nazev=char('Crossvalidation','Fourier','Mallows','Plug-in');
            
            for i=1:4
                c2='set(hL(6:16),''value'',0);set(gco,''value'',1);';
                hL(12+i)=uicontrol('Style','radiobutton','Units','normalized', ...
                    'FontSize',round(pos(4)*10)+6, ...
                    'FontUnits','normalized',...
                    'Position',[kraj,.5-(i-1)*.06,.3,.05], ...
                    'Callback',[c1,c2],...
                    'String',nazev(i,:));
            end;
            
            %Menu Handle
            hL(17)=uicontrol('Style','push','Units','normalized', ...
                'FontSize',4*round(pos(4)*10)/3+5, ...
                'FontUnits','normalized',...
                'Position',[.5,.09,.2,.15], ...
                'Callback','alg(''prac'');',...
                'Enable','off',...
                'BackgroundColor',[.5,0.5,0.5], ...
                'String','Compute');
            
            %Close Handle
            uicontrol('Style','push','Units','normalized', ...
                'FontSize',4*round(pos(4)*10)/3+5, ...
                'FontUnits','normalized',...
                'Position',[.72,.09,.2,.15], ...
                'Callback','alg(''close'');',...
                'BackgroundColor',[.5,0.5,0.5], ...
                'String','Close');
            
            %K min Handle
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'HorizontalAlignment','left',...
                'Position',[.05,.18,.2,.06], ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'BackgroundColor',col,...
                'String','Set k min: ');
            
            hL(18)=uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontWeight','bold',...
                'FontUnits','normalized',...
                'BackgroundColor',col, ...
                'Position',[.25,.18,.07,.06], ...
                'String','2',...
                'HorizontalAlignment','right');
            
            c1='hL=get(gcf,''UserData'');hod=get(gco,''Value'');';
            c2='set(hL(18),''string'',num2str(hod));';
            hL(19) = uicontrol('Style','slider', 'Units', 'normalized',...
                'FontUnits','normalized',...
                'Position',[.33,.18,.12,.06], ...
                'Min',2,'Max',20,...
                'sliderstep',[1/9 222], ...
                'Callback',[c1,c2],...
                'Value',2);
%                'sliderstep',[1/9 222], ...
            
            %K max Handle
            uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'HorizontalAlignment','left',...
                'Position',[.05,.09,.2,.06], ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontUnits','normalized',...
                'BackgroundColor',col,...
                'String','Set k max: ');
            
            hL(20)=uicontrol( ...
                'Style','text', ...
                'Units','normalized', ...
                'FontSize',round(pos(4)*10)+7, ...
                'FontWeight','bold',...
                'FontUnits','normalized',...
                'BackgroundColor',col, ...
                'Position',[.25,.09,.07,.06], ...
                'String','12',...
                'HorizontalAlignment','right');
            
            
            c2='set(hL(20),''string'',num2str(hod));';
            hL(21) = uicontrol('Style','slider', 'Units', 'normalized',...
                'FontUnits','normalized',...
                'Position',[.33,.09,.12,.06], ...
                'Min',2,'Max',20,...
                'sliderstep',[1/9 222], ...
                'Callback',[c1,c2],...
                'Value',12);
            
            set(gcf,'UserData',hL);
            
        end;
        
        set(gcf,'UserData',hL);
        
    case 'prac'
        hL=get(gcf,'UserData');
        
        iind=get(hL(1:4),'Value');
        iest=find(cat(1,iind{:}));
        
        iind=get(hL(6:16),'Value');
        imet=find(cat(1,iind{:}));
        
        iind=get(hL([19,21]),'Value');
        kbound=(cat(1,iind{:}))';
        
        load w;
        nHndl=hndlList(1);
        kHndl=hndlList(2);
        mHndl=hndlList(3);
        hHndl=hndlList(4);
        set(hHndl,'String','wait...');
        pause(.1);
        [k,h]=algor(x,y,iest,imet,kbound);
        set(nHndl,'Value',1);
        set(kHndl,'Value',k-1);
        set(mHndl,'Value',2);
        set(hHndl,'String',num2str(h));
        zadani('prac');
        
    case 'close'
        set(gcf,'closerequestfcn','closereq');
        delete(gcf);
end;



